package metadata;

import java.util.*;

import utility.*;

public class Key {
	
	//private vars
	private String name						= "";
	private Map<Integer, Column> columns	= null;
	
	private HashBucket index				= null;
	
	//constructors
	public Key(String name) {
		this.name 	= name;
		
		columns 	= new HashMap<Integer, Column>();
	}
	
	//util methods
	public void printIndex() {	index.printStructure(); }
	
	//simple getters
	public String getName() 		{ return name; 	}
	public HashBucket getIndex() 	{ return index;	}
	
	
	//simple setters
	public void setName(String name) { this.name = name; }
	
	
	//complex getters
	public Set<Column> getColumnsUnordered() {
		return new HashSet<Column>(columns.values());
	}
	
	public Column[] getColumnsOrdered() {
		Column[] columnArray = new Column[columns.size()];
		
		for(int i = 0; i < columns.size(); ++i) {
			columnArray[i] = columns.get(i);
		}
		
		return columnArray;
	}
	
	public List<String> getValuesAt(int index) {
		List<String> dataValues = new ArrayList<String>();
		for(int j = 0; j < columns.size(); ++j) {
			dataValues.add(columns.get(j).getValue(index));
		}
		return dataValues;
	}
	
	//complex setters
	
	public void setColumnAt(Column column, Integer index) {
		columns.put(index, column);
	}
	
	//DML
	public void init() {
		index = new HashBucket(columns.size());
	}
	
//	public void init(int rowCount) {
//		init();
//		indexAtFor(0, rowCount);
//	}
//	
//	public void indexAt(int indexStart) {
//		indexAtFor(indexStart, 1);
//	}
	
	public void indexAtFor(int indexStart, int amount) {
		int indexStop = (indexStart + amount);
		for(int indexPos = indexStart; indexPos < indexStop; ++indexPos) {
			index.setIndexEntry(getValuesAt(indexPos), indexPos);
		}
	}
	
	public void removeIndexAt(int indexPos) {
		index.removeIndexEntry(getValuesAt(indexPos));
	}
}
